package com.atguigu.spzx.manager.service.impl;

import com.atguigu.spzx.common.utils.DateUtils;
import com.atguigu.spzx.manager.mapper.OrderStatisticsMapper;
import com.atguigu.spzx.manager.entity.OrderStatistics;
import com.atguigu.spzx.manager.service.OrderStatisticsService;
import com.atguigu.spzx.manager.vo.orderStatistics.OrderStatisticsReqVo;
import com.atguigu.spzx.manager.vo.orderStatistics.OrderStatisticsRespVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * OrderStatisticsServiceImpl
 * </p>
 *
 * @author flyduck
 * @since 2023/11/2
 */
@Service
public class OrderStatisticsServiceImpl implements OrderStatisticsService {

    @Autowired
    private OrderStatisticsMapper orderStatisticsMapper;

    /**
     * 订单统计
     * @param reqVo
     * @return
     */
    @Override
    public OrderStatisticsRespVo statisticsOrderByQuery(OrderStatisticsReqVo reqVo) {
        //根据orderStatisticsReqVo条件查询list数据
        List<OrderStatistics> orderStatisticsList = orderStatisticsMapper.getOrderStatisticsListByQuery(reqVo);

        //遍历list集合，将所有日期封装到一个集合，将所有金额封装到一个集合
        List<String> dateList = orderStatisticsList.stream()
                .map(orderStatistics -> DateUtils.getYYYY_MM_DD(orderStatistics.getOrderDate()))
                .collect(Collectors.toList());
        List<BigDecimal> amountList = orderStatisticsList.stream()
                .map(OrderStatistics::getTotalAmount)
                .collect(Collectors.toList());

        OrderStatisticsRespVo respVo = new OrderStatisticsRespVo();
        respVo.setDateList(dateList);
        respVo.setAmountList(amountList);
        return respVo;
    }
}
